import { reactive } from 'vue'

export const countDownInfo = reactive({
  text: '',
  time: 0,
  running: false,
  clock: null
})

export const initCountDownInfo = (time = 60, text = '获取验证码') => {
  clearInterval(countDownInfo.clock)
  countDownInfo.text = text
  countDownInfo.time = time
  countDownInfo.running = false
}

export const countDown = reset => {
  countDownInfo.clock = setInterval(() => {
    if (countDownInfo.time <= 1) {
      reset()
    } else {
      countDownInfo.running = true
      countDownInfo.time--
      countDownInfo.text = `${countDownInfo.time}秒后重试`
    }
  }, 1000)
}
